//
//  Roster.m
//  VKChat
//
//  Created by Максим Бризинский on 31.01.11.
//  Copyright 2011 БГУИР. All rights reserved.
//

#import "Roster.h"


@implementation Roster
@synthesize onlineFriends = _onlineFriends;
@synthesize allFriends = _allFriends;
@synthesize countOnline = _countOnline;
@synthesize countOffline = _countOffline;
@synthesize delegate = _delegate;

-(void)didFriendOnlineWithUid:(long) uid {
	User* user = [self searchUserForUid:uid];
	[_onlineFriends addObject:user];
	[_delegate userOnline:user];
	_countOnline++;
	NSLog(@"Пользователь: %@ %@ теперь online",user.firstName, user.lastName);
}

-(void)didFriendOfflineWithUid:(long) uid  {
	int count = 0;
	BOOL found = NO;
	for (User* searchingUser in _onlineFriends) {
		if (searchingUser.uid == uid) {
			[_delegate userOffline:searchingUser];
			found = YES;
			NSLog(@"Пользователь: %@ %@ теперь offline",searchingUser.firstName, searchingUser.lastName);
			break;
		}
		count++;
	}
	if (found) {
		[_onlineFriends removeObjectAtIndex:count];
		_countOnline--;
	}
}

-(User*) searchUserForUid:(long) uid {
	for (User* user in _allFriends) {
		if (user.uid == uid)
			return user;
	}
	return nil;
}

@end
